package com.syni.mdd.yhd.common.all.utils;

import com.syni.mdd.yhd.common.all.constants.Constants;
import com.syni.mdd.sjb.common.component.utils.common.utils.DateTimeUtils;
import com.syni.mdd.sjb.common.component.utils.redis.utils.RedisTemplateUtils;
import lombok.extern.log4j.Log4j2;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.TimeUnit;

;

/**
 * @className OrderUtils
 * @description TOO
 * @Author cfx
 * @DATE 2019/6/25 15:41
 * @VERSION 1.0
 **/
@Log4j2
public class OrderUtils {
    final static int MAX_BUSINESS_ID = 6;

    public static Set groupByNo = new HashSet();

    public static String getOrderNo(Integer businessId){
        String businessIdNo = completionNo(businessId.toString(),MAX_BUSINESS_ID);
        String timeStampTip = DateTimeUtils.converDateToString(new Date(),DateTimeUtils.DATE_PATTERN_TIMESTAMP_01);
        int random = (int)(Math.random() * 100);
        String randoms = String.valueOf(random);
        if(random < 10){
            randoms = "0" + randoms;
        }
        return String.format("%s%s%s",businessIdNo,timeStampTip,randoms);
    }

    public static String getGroupBuyPayNo(boolean isMax){
        //重复尝试1万次
        int MAX_TRY = 10000;
        if(isMax){
            MAX_TRY = Integer.MAX_VALUE;
        }
        int count = 0;
        String coupnCode = getCouponCode();
        while (count < MAX_TRY) {
            count++;
            if (!RedisTemplateUtils.getRedisTemplate().opsForValue().
                    setIfAbsent(Constants.REDIS_KEY_USE_CODE + coupnCode, 1, 1, TimeUnit.DAYS)) {
                coupnCode = getCouponCode();
            }
            break;
        }
        return coupnCode;
    }

    public static String getCouponCode(){
        final int MAX_NUM = 16;
        Date nowTime = new Date();
        String timestamp = DateTimeUtils.converDateToString(nowTime,DateTimeUtils.DATE_PATTERN_DAY_04);
        int seekNeed = MAX_NUM - timestamp.length();
        int randomMax = (int) (Math.pow(10, seekNeed));
        int random = (int) (Math.random() * randomMax);
        String tureRandom = completionNo(String.valueOf(random),seekNeed);
        String couponCode = String.format("%s%s", timestamp, tureRandom);
        return couponCode;
    }

    public static void main(String[] args) {
        System.out.println(getCouponCode());
    }

    public static String completionNo(String no,Integer num){
        StringBuilder prefix = new StringBuilder();
        Integer length = no.length();
        if(length < num) {
            for(int ii=0;ii<num - length;ii++){
                prefix.append("0");
            }
        }
        prefix.append(no);
        return prefix.toString();
    }

    public static String getRandomNo(String pre,Integer num){
        String timeStampTip = DateTimeUtils.converDateToString(new Date(),DateTimeUtils.DATE_PATTERN_TIMESTAMP_01);
        int randomLength = num - pre.length() - timeStampTip.length();
        int random = (int)(Math.random() * randomLength);
        String randoms = String.valueOf(random);
        if(random < 10){
            randoms = "0" + randoms;
        }
        return String.format("%s%s%s",pre,timeStampTip,randoms);
    }
}
